package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.dao.StatisticsDao;
import com.my12306.train.dao.TicketDao;
import com.my12306.train.model.entity.TicketEntity;
import com.my12306.train.model.vo.ChartBarVo;
import com.my12306.train.model.vo.ChartLineVo;
import com.my12306.train.model.vo.StatisticsVo;
import com.my12306.train.model.vo.TotalStatisticsVo;
import com.my12306.train.service.StatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("statisticsService")
public class StatisticsServiceImpl extends ServiceImpl<StatisticsDao, StatisticsVo> implements StatisticsService {

    @Override
    public List<StatisticsVo> queryChartsParams() {
        List<StatisticsVo> statisticsVos = this.baseMapper.queryChartsParams();
        statisticsVos.stream().map(x -> {
            String routeMsg = x.getRouteStartStation() + "-" + x.getRouteEndStation();
            x.setRouteMsg(routeMsg);
            return x;
        }).distinct().collect(Collectors.toList());
        return statisticsVos;
    }

    @Override
    public ResponseData queryFiveRoutes() {
        List<StatisticsVo> statisticsVos = this.baseMapper.queryFiveRoutes();
        List<String> list = new ArrayList<>();
        ArrayList<ChartLineVo> chartLineVos = new ArrayList<>();
        statisticsVos.stream().forEach(x -> {
            String routeMsg = x.getRouteStartStation() + "-" + x.getRouteEndStation();
            list.add(routeMsg);
            ChartLineVo chartLineVo = new ChartLineVo();
            chartLineVo.init("line");
            chartLineVo.setName(routeMsg);
            List<Integer> chartLineVoData = chartLineVo.getData();
            List<StatisticsVo> vos = this.baseMapper.queryOneRouteData(x.getRouteId());
            vos.stream().forEach(vo->{
                chartLineVoData.set(vo.getMonth()-1, vo.getCount());
            });
            chartLineVo.setData(chartLineVoData);
            System.out.println(chartLineVo);
            chartLineVos.add(chartLineVo);
        });
        return ResponseData.ok().put("routeList", list).put("dataList", chartLineVos);
    }

    @Override
    public ResponseData queryFiveStations() {
        List<StatisticsVo> statisticsVos = this.baseMapper.queryFiveStations();
        List<String> stationList = new ArrayList<>();
        List<ChartBarVo> chartBarVos = new ArrayList<>();
        List<List<StatisticsVo>> stationsData = new ArrayList<>();
        statisticsVos.stream().forEach(x -> {
            String stationName = x.getStartStation();
            stationList.add(stationName);

            List<StatisticsVo> vos = this.baseMapper.queryOneStationData(x.getStartStation());
            stationsData.add(vos);
        });

        for (int i = 0; i < 12; i++) {
            ChartBarVo chartBarVo = new ChartBarVo();
            chartBarVo.init("bar");
            List<Double> moneyList = new ArrayList<>();
            chartBarVos.add(chartBarVo);
        }
        Double[][] moneyData = new Double[12][5];
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 5; j++) {
                moneyData[i][j] = 0.0;
            }
        }
        System.out.println(moneyData.toString());
        for (int j = 0; j < stationsData.size(); j++) {
            for (int i = 0; i < stationsData.get(j).size(); i++) {
                moneyData[stationsData.get(j).get(i).getMonth() - 1][j] = stationsData.get(j).get(i).getMoney();
            }
        }
        for (int i = 0; i < chartBarVos.size(); i++) {
            chartBarVos.get(i).setData(moneyData[i]);
        }
        return ResponseData.ok().put("stationList", stationList).put("dataList", chartBarVos).put("moneyData",moneyData);
    }

    @Override
    public ResponseData queryTenStations() {
        List<String> stationList = new ArrayList<>();
        List<StatisticsVo> statisticsVos = this.baseMapper.queryTenStations();
        statisticsVos.stream().forEach(x->{
            String startStation = x.getStartStation();
            stationList.add(startStation);
        });
        List<TotalStatisticsVo> totalStatisticsList = queryTotalStatistics();
        return ResponseData.ok().put("dataList", statisticsVos).put("stationList", stationList).put("totalStatisticsList",totalStatisticsList.get(0));
    }

    @Override
    public List<TotalStatisticsVo> queryTotalStatistics() {
        return this.baseMapper.queryTotalStatistics();
    }
}
